<?php

namespace App\Admin\Controllers\Configs;

use App\Admin\Repositories\CdnDomain;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CdnDomainController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CdnDomain(), function (Grid $grid) {
            $grid->model()->whereIn('type',[1,3,0]);
            $grid->column('id')->sortable();
            $grid->column('domain');
            $grid->column('type')->using([1 => '图片CDN',3 => '图片直连',0=>'通用'])
            ->dot(
                [
                    1 => 'warning',
                    3 => 'primary',
                    0 => 'success',
                ], 
                'primary' // 第二个参数为默认值
            )->filter();
            $grid->column('state','是否启用')
            ->if(function ($column) {
                if($column->getValue() == -1){
                    $column->using([-1 => '被封禁'])->label('danger');
                }
                else{
                    $column->switch();
                }
            })->sortable();

            $grid->column('mark')->editable();
            $grid->column('server')->sortable();
            $grid->column('created_at')->sortable();
        
            $grid->enableDialogCreate();
            $grid->quickSearch(['domain']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('domain');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CdnDomain(), function (Show $show) {
            $show->field('id');
            $show->field('domain');
            $grid->column('state');
            $show->field('server');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CdnDomain(), function (Form $form) {
            $form->display('id');
            $form->text('domain')->required();
            $form->radio('type')->options([1 => '图片CDN',3 => '图片服务器直连', 0=>'通用域名'])->default(1);
            $form->switch('state','是否启用')->options([1 => '启用', 0=> '停用'])->default(1);
            $form->text('server');
            $form->text('mark','备注');
        
            $form->display('created_at');
            $form->display('updated_at');

            //编辑缓存同步
            $form->saved(function(Form $form){
                if($form->model()->type == 0 || $form->model()->type == 1){
                    cache()->forget('img_cdn_count');
                    cache()->forget('img_cdn1');
                    cache()->forget('img_cdn2');
                }
                if($form->model()->type == 0 || $form->model()->type == 2){
                    cache()->forget('video_cdn_count');
                    cache()->forget('video_cdn1');
                    cache()->forget('video_cdn2');
                }else{
                    cache()->forget('img_domain');
                }
                
            });

            $form->deleted(function (Form $form, $result) {
                // 获取待删除行数据，这里获取的是一个二维数组
                $data = $form->model()->toArray();
                
                // 通过 $result 可以判断数据是否删除成功
                if ( $result) {
                    cache()->forget('img_domain');
                    cache()->forget('img_cdn_count');
                    cache()->forget('video_cdn_count');
                    cache()->forget('img_cdn1');
                    cache()->forget('img_cdn2');
                    cache()->forget('video_cdn1');
                    cache()->forget('video_cdn2');
                    // 返回删除成功提醒，此处跳转参数无效
                    return $form->response()->success('删除成功');
                }
                return $form->response()->error('数据删除失败');
            });
        });
    }
}
